package com.gepengjun.lims.controler;


import com.baomidou.mybatisplus.plugins.Page;
import com.gepengjun.lims.entity.RefPermissionRole;
import com.gepengjun.lims.entity.Role;
import com.gepengjun.lims.pojo.Node;
import com.gepengjun.lims.service.RefPermissionRoleService;
import com.gepengjun.lims.service.RoleService;
import com.gepengjun.lims.util.JsonResult.Result;
import com.gepengjun.lims.util.JsonResult.ResultUtil;
import com.gepengjun.lims.util.common.CommonUtil;
import com.gepengjun.lims.util.common.DataType;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jun
 * @since 2018-08-19
 */
@Controller
@RequestMapping("/lims/role")
public class RoleController {

    protected static final Logger logger = LoggerFactory.getLogger(RoleController.class);


    @Autowired
    private RoleService roleService;

    @Autowired
    private RefPermissionRoleService refPermissionRoleService;


    @GetMapping("/roleList")
    public String roleList(Page<Role> page, Role role, ModelMap map){
        page = roleService.getRolePage(page,role);
        map.addAttribute("role",role);
        map.addAttribute("page",page);
        return "role/roleList";
    }

    @GetMapping("/addRolePage")
    public String addRolePage(){
        return "role/addRole";
    }

    @PostMapping("/addRole")
    @ResponseBody
    public Result addRole(Role role){
        if (roleService.insert(role)){
            return ResultUtil.success("添加角色成功");
        }
        return ResultUtil.error(-2,"添加失败");
    }


    @PostMapping("/batchDisableRoles")
    @ResponseBody
    public Result batchDisableRoles(String roleIds){
        if (StringUtils.isNotEmpty(roleIds)){
            List<Long> userIdList = (List<Long>) CommonUtil.covertArrayToList(roleIds.split(","), DataType.ISLONG);
             if (roleService.batchDisableUsers(userIdList)){
                 return ResultUtil.success("禁用成功");
             }
        }
        return ResultUtil.error(-2,"禁用失败");
    }

    @PostMapping("/batchAbleRoles")
    @ResponseBody
    public Result batchAbleRoles(String roleIds){
        if (StringUtils.isNotEmpty(roleIds)){
            List<Long> userIdList = (List<Long>) CommonUtil.covertArrayToList(roleIds.split(","), DataType.ISLONG);
            if (roleService.batchAbleRoles(userIdList)){
                return ResultUtil.success("启用成功");
            }
        }
        return ResultUtil.error(-2,"启用失败");
    }

    @GetMapping("/updRolePage")
    public String updRolePage(Role role,ModelMap map){
        role = roleService.selectById(role.getRoleId());
        map.addAttribute("role",role);
        return "role/updRole";
    }

    @PostMapping("updRole")
    @ResponseBody
    public Result updRole(Role role){
        if (role.getRoleId() != null){
            if (roleService.updateById(role)){
                return ResultUtil.success("修改成功");
            }
        }
        return ResultUtil.error(-1,"修改失败");

    }

    @GetMapping("/roleAuthorizePage")
    public String roleAuthorizePage(Role role,ModelMap map){
        map.addAttribute("role",role);
        return "role/roleAuthorize";
    }

    @PostMapping("/gerRolePermissions")
    @ResponseBody
    public List<Node> gerRolePermissions(Role role){
        logger.info(role.getRoleId().toString());
        List<Node> nodeList = roleService.gerRolePermissions(role.getRoleId());
        logger.info(nodeList.toString());
        return nodeList;
    }



}

